package Case;
//同步代码块演示
//需求：假如电影院共有100张电影票需要出售，在门口一共有三台售票机同时售票，请设计一款程序模拟售票
public class Demo1 {
    static int ticket=0;//声明100张票,设为静态属性,确保只有一份
    static final Object ob=new Object();

    public static void main(String[] args) {
        Runnable sales=()-> {
            while (true){
              synchronized (Demo1.class){
                  Thread.yield();
                  try {
                      Thread.sleep(200);
                  } catch (InterruptedException e) {
                      throw new RuntimeException(e);
                  }
                  if(ticket<100){
                      ticket++;
                      String name=Thread.currentThread().getName();
                      System.out.println(name+"售出:"+ticket+"号票");
                }else {
                    break;
                }
              }
            }
        };
    new Thread(sales,"机器1号").start();
    new Thread(sales,"机器2号").start();
    new Thread(sales,"机器3号").start();
    }

}
